﻿namespace WPFDBBrowser.Windows
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using WPFDBBrowser.Helpers;
    using WPFDBBrowser.Interfaces;
    using WPFDBBrowser.Settings;

    /// <summary>
    /// Interaction logic for ConnectionEdit.xaml
    /// </summary>
    public partial class ConnectionEdit : Window
    {
        public static RoutedCommand TestConnection = new RoutedCommand();

        public ConnectionEdit()
            : this(null)
        {
        }

        public ConnectionEdit(DBConnectionInfo connectionInfo)
        {
            InitializeComponent();

            this.CommandBindings.Add(new CommandBinding(TestConnection, this.ExecutedTestConnectionCommand, this.CanExecuteTestCommand));
            this.btnTest.Command = TestConnection;

            this.DBInfo = connectionInfo;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                if (this.DBInfo == null)
                    this.DBInfo = new DBConnectionInfo();
                this.comboBoxTypes.SelectionChanged += new SelectionChangedEventHandler(comboBoxTypes_SelectionChanged);
                this.Loaded += new RoutedEventHandler(ConnectionEdit_Loaded);
            }
        }

        public DBConnectionInfo DBInfo { get; set; }

        private void ExecutedTestConnectionCommand(object sender,
            ExecutedRoutedEventArgs e)
        {
            try
            {
                (this.comboBoxTypes.SelectedItem as IDBFactory).CheckConnection(this.txtConnectionString.Text);
            }
            catch
            {
                // TODO: EXCEPTION HANDLING needs to be shown some dialog
            }
        }

        private void CanExecuteTestCommand(object sender,
            CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.comboBoxTypes.SelectedItem != null &&
                !string.IsNullOrEmpty(this.txtConnectionString.Text);
        }

        void comboBoxTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBoxTypes.SelectedItem != null &&
                this.comboBoxTypes.SelectedItem is IDBFactory)
            {
                this.comboBoxTypes.ToolTip = (this.comboBoxTypes.SelectedItem as IDBFactory).FriendlyName;
            }
        }

        void ConnectionEdit_Loaded(object sender, RoutedEventArgs e)
        {
            List<IDBFactory> dbFactories = new List<IDBFactory>(UnityContainerFactory.GetInstance().ResolveAll<IDBFactory>());
            comboBoxTypes.ItemsSource = dbFactories;
            // Set the data context of control to DBInfo, we need to bind controls to it
            if (this.DBInfo != null)
                this.DataContext = this.DBInfo;

            if (comboBoxTypes.Items.Count > 0 && comboBoxTypes.SelectedIndex < 0)
                comboBoxTypes.SelectedIndex = 0;
        }

        private void btnOKClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void btnCancelClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
